#define DEBUG
#include <cstdio>
#include <algorithm>

using namespace std;

typedef long long ll;

const int maxn=1000000, mods=1000000007;

class RMQ{
public:
  static const int maxlogn=20;
  int v[maxn+1][maxlogn+1], book[maxn+1], n;

  void init(int n, int a[]) {
    this->n = n;
    v[1][0] = a[1];
    for (int i=2; i<=n; i++) {
      book[i] = book[i/2]+1;
      v[i][0] = a[i];
    }
    for (int j=1; j<=maxlogn; j++) {
      for (int i=1; i<=n; i++) {
        if (i+(1<<(j-1))<=n) {
          v[i][j] = max(v[i][j-1], v[i+(1<<(j-1))][j-1]);
        } else {
          v[i][j] = v[i][j-1];
        }
      }
    }
  }

  inline int get(int l, int r) {
    int t=book[r-l+1];
    return max(v[l][t], v[r-(1<<t)+1][t]);
  }

  int findPrev(int p, int lim) {
    int ret=0;
    for (int l=1, r=p-1, mid; l<=r; ) {
      mid = (l+r)/2;
      if (get(mid, p-1)>=lim) {
        ret = mid;
        l = mid+1;
      } else {
        r = mid-1;
      }
    }
    return ret;
  }

  int findNext(int p, int lim) {
    int ret=n+1;
    for (int l=p+1, r=n, mid; l<=r; ) {
      mid = (l+r)/2;
      if (get(p+1, mid)>=lim) {
        ret = mid;
        r = mid-1;
      } else {
        l = mid+1;
      }
    }
    return ret;
  }
};

class Ele {
public:
  int id, v;

  static bool cmp(Ele &a, Ele &b) {
    return a.v>=b.v;
  }
};

int main() {
  freopen("three.in", "r", stdin);
  freopen("three.out", "w", stdout);

  static int a[maxn+1];
  static Ele ele[maxn+1];
  static RMQ rmq;
  int n;
  scanf("%d", &n);
  for (int i=1; i<=n; i++) {
    scanf("%d", a+i);
    ele[i].id = i, ele[i].v = a[i];
  }
  rmq.init(n, a);

  ll ans=0;
  for (int i=1; i<=n; i++) {
    ll t=ele[i].id, u=rmq.findPrev(t, a[t]), v=rmq.findNext(t, a[t]), l=rmq.findPrev(u, a[t]), r=rmq.findNext(v, a[t]);
    if (u>0 && v<=n) {
      ans = ((u-l)*(r-v)%mods*a[u]%mods*a[v]%mods*a[t]%mods+ans)%mods;
    }
    if (l>0) {
      ans = ((l-rmq.findPrev(l, a[t]))*(v-t)%mods*a[l]%mods*a[u]%mods*a[t]%mods+ans)%mods;
    }
    if (r<=n) {
      ans = ((t-u)*(rmq.findNext(r, a[t])-r)%mods*a[t]%mods*a[v]%mods*a[r]%mods+ans)%mods;
    }
  }

  printf("%lld", ans);

  fclose(stdin);
  fclose(stdout);
  return 0;
}
